package org.jeecg.modules.bj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.entity.ActZprocess;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.activiti.service.Impl.ActZprocessServiceImpl;
import org.jeecg.modules.bj.entity.BjRecruitmentApplication;
import org.jeecg.modules.bj.mapper.BjRecruitmentApplicationMapper;
import org.jeecg.modules.bj.service.IBjRecruitmentApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: bj_recruitment_application
 * @Author: jeecg-boot
 * @Date:   2021-10-21
 * @Version: V1.0
 */
@Service
public class BjRecruitmentApplicationServiceImpl extends ServiceImpl<BjRecruitmentApplicationMapper, BjRecruitmentApplication> implements IBjRecruitmentApplicationService {

    @Autowired
    ActBusinessServiceImpl actBusinessService;
    @Autowired
    private ActZprocessServiceImpl actZprocessService;

    @Override
    public void saveData(BjRecruitmentApplication bjRecruitmentApplication){
        bjRecruitmentApplication.setActStatus("草稿");
        save(bjRecruitmentApplication);
    }

    @Override
    public ActBusiness saveActBusinessBeforeApprove(String tableId){
       LambdaQueryWrapper<ActZprocess> wrapper = new LambdaQueryWrapper<ActZprocess>();
        wrapper.orderByAsc(ActZprocess::getProcessKey).orderByDesc(ActZprocess::getVersion);
        wrapper.eq(ActZprocess::getLatest, 1);
        wrapper.eq(ActZprocess::getName, "招工申请");
        ActZprocess actZprocess = actZprocessService.getOne(wrapper);
        String tableName = "bj_recruitment_application";
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String username = sysUser.getUsername();
        ActBusiness actBusiness = new ActBusiness();
        actBusiness.setUserId(username);
        actBusiness.setTableId(tableId);
        actBusiness.setProcDefId(actZprocess.getId());
        actBusiness.setTitle("招工申请");
        actBusiness.setTableName(tableName);
        actBusinessService.save(actBusiness);
        return actBusiness;
    }

    @Override
    public String deleteActBusinessById(String actbussinessid){
        actBusinessService.removeById(actbussinessid) ;
        return "sucess";
    }

}
